﻿using Architecture.Common;
using AuthCenter.Domain;
using AuthCenter.ErorCode;
using AuthCenter_API.Read.Applications.Commands.Audit;
using AuthCenter_API.Read.Applications.Commands.RBAC;
using AuthCenter_API.Read.Applications.Commands.User;
using AuthCenter_API.Read.Dtos;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace AuthCenter_API.Read.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ManagementController : ControllerBase
    {
        private readonly ILogger<ManagementController> logger;
        private readonly IMediator mediator;

        public ManagementController(ILogger<ManagementController> logger, IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
        }
        /// <summary>
        /// 获取省份
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<ShengModel>>> GetSheng()
        {
            try
            {
                logger.LogInformation("获取省份成功");
                ShengQueryCommand command = new ShengQueryCommand();
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("获取省份失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 获取县
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<XianModel>>> GetXian(int shiId)
        {
            try
            {
                //string numberstr = "123456";
                //int number = numberstr.ToInt32();
                logger.LogInformation("获取县成功");
                XianQueryCommand command = new XianQueryCommand();
                command.ShiId = shiId;
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("获取县失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 获取市
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<ShiModel>>> GetShi(int shengId)
        {
            try
            {
                logger.LogInformation("获取市成功");
                ShiQueryCommand command = new ShiQueryCommand();
                command.ShengId = shengId;
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("获取市失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 查询用户
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<APIPaging<UserDto>>> GetUser(int index, int size)
        {
            try
            {
                logger.LogInformation("查询用户成功");
                UserQueryCommand command = new UserQueryCommand();
                command.Index = index;
                command.Size = size;
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("查询用户失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<APIResult<object>> Login(string? userName, string? password)
        {
            try
            {
                logger.LogInformation("登录成功");
                var userId=HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                var username = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
                var rolrName = HttpContext.User.FindFirst(ClaimTypes.Role)?.Value;
                var customerValue = HttpContext.User.FindFirst("CustomClaimType")?.Value;
                var key = HttpContext.User.FindFirst("key")?.Value;

                UserLoginCommand command = new UserLoginCommand();
                command.UserName = userName;
                command.Password = password;
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("登录失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 查询角色
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<APIPaging<RoleModel>>> GetRole(int index,int size)
        {
            try
            {
                logger.LogInformation("查询角色成功");
                RoleQueryCommand command = new RoleQueryCommand();
                command.Index = index;
                command.Size = size;
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("查询角色失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 通过部门id查询用户（员工）
        /// </summary>
        /// <param name="depId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<UserModel>>> GetDep(int depId)
        {
            try
            {
                try
                {
                    logger.LogInformation("查询成功");
                    DepQueryCommand command = new DepQueryCommand();
                    command.DepId = depId;
                    return await mediator.Send(command);
                }
                catch (Exception ex)
                {
                    logger.LogError("查询失败" + ex.Message);
                    throw;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 反填用户名称
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<UserNameDto>> GetUserName(int roleId)
        {
            FantianUserNameCommand command = new FantianUserNameCommand();
            command.RoleId = roleId;
            return await mediator.Send(command);
        }
        /// <summary>
        /// 查询父级（下拉）
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<MenuDto>>> GetParentName()
        {
            ParentNameQueryCommand command = new ParentNameQueryCommand();
            return await mediator.Send(command);
        }
        /// <summary>
        /// 查询菜单
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<APIPaging<MenuDto>>> GetMenu(int index,int size)
        {
            MenuQueryCommand command = new MenuQueryCommand();
            command.Index = index;
            command.Size = size;
            return await mediator.Send(command);
        }
        /// <summary>
        /// 查询角色名称用于分配权限
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<RoleModel>>>GetRoleName()
        {
            RoleNameQueryCommand command = new RoleNameQueryCommand();
            return await mediator.Send(command);
        }
        /// <summary>
        /// 查询菜单名称用于分配权限
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<TreeSelectDto>>> GetMenuName()
        {
            MenuTreeQueryCommand command = new MenuTreeQueryCommand();
            return await mediator.Send(command);
        }
        /// <summary>
        /// 反填角色菜单
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<object>> GetRoleMenu(int roleId)
        {
            FantianRoleMenuCommand command = new FantianRoleMenuCommand();
            command.RoleId = roleId;
            return await mediator.Send(command);
        }
        /// <summary>
        /// 根据角色id查询查单（用于登录进去看菜单）
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<RoleMenuDto>>> GetRoleMenuLogin(string roleId)
        {
            RoleMenuQueryCommand command=new RoleMenuQueryCommand();
            command.RoleId=roleId;
            return await mediator.Send(command);
        }

        /// <summary>
        /// 查询审批
        /// </summary>
        /// <param name="index">码数</param>
        /// <param name="size">条数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<APIPaging<AuditDto>>> GetAudit([FromQuery]AuditQueryCommand command)
        {
            return await mediator.Send(command);
        }
        /// <summary>
        /// 查询部门信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<List<DepModel>>> GetAuditDep()
        {
            AuditDepQueryCommand command = new AuditDepQueryCommand();
            return await mediator.Send(command);
        }
    }
}
